package com.example.tse.rbyair.login;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.tse.rbyair.R;
import com.example.tse.rbyair.base.RbBaseActivity;
import com.example.tse.rbyair.login.adaper.MainFragmentPagerAdapter;
import com.example.tse.rbyair.login.fragment.LoginFragment;
import com.example.tse.rbyair.login.fragment.ReginsterFragment;
import com.example.tse.rbyair.tsedemo.DemoListActivity;

import org.kymjs.kjframe.utils.DensityUtils;

import java.util.ArrayList;

/**
 * Created by tse on 16/1/15.
 */

public class LoginActivity extends RbBaseActivity {

    private ImageView imageView1;
    private TextView loginTxt;
    private TextView registerTxt;

    private ImageView loginSelectedImg;


    private ViewPager viewpagerLogin;
    //动画图片宽度
    public int bmpW;

    private int moveI = 0;
    private int w1 = 0;

    private RelativeLayout.LayoutParams layoutParams;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_login_activity);

        imageView1 = (ImageView) findViewById(R.id.imageView1);
        loginTxt = (TextView) findViewById(R.id.loginTxt);
        registerTxt = (TextView) findViewById(R.id.registerTxt);
        loginSelectedImg = (ImageView) findViewById(R.id.loginSelectedImg);
        viewpagerLogin = (ViewPager) findViewById(R.id.viewpagerLogin);

        InitViewPager();
        bmpW = BitmapFactory.decodeResource(getResources(), R.drawable.tag_triangle).getWidth();// 获取图片宽度
        layoutParams = (RelativeLayout.LayoutParams) loginSelectedImg.getLayoutParams();

        loginTxt.setOnClickListener(new MyOnClickListener(0));
        registerTxt.setOnClickListener(new MyOnClickListener(1));

        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, DemoListActivity.class));
            }
        });
    }



    /**
     * 初始化ViewPager
     */
    private void InitViewPager() {
        ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new LoginFragment());
        fragmentList.add(new ReginsterFragment());

        MainFragmentPagerAdapter fragmentAdapter = new MainFragmentPagerAdapter(this.getSupportFragmentManager(), fragmentList);
        viewpagerLogin.setAdapter(fragmentAdapter);
        viewpagerLogin.setCurrentItem(0);
        viewpagerLogin.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 0) {
                    moveI = (int) ((((double) positionOffsetPixels / DensityUtils.getScreenW(LoginActivity.this) * (DensityUtils.getScreenW(LoginActivity.this) - bmpW - 2 * DensityUtils.dip2px(LoginActivity.this, 68)))));
                    layoutParams.leftMargin = moveI + DensityUtils.dip2px(LoginActivity.this, 68);
                } else {

                }

                loginSelectedImg.setLayoutParams(layoutParams);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            viewpagerLogin.setCurrentItem(index);
        }
    }

}
